package tothreedesignmodel.composite;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author lzhphantom
 * @description
 * @date 创建于 18:31 2020/8/1
 * @modified
 */
@Data
public class Bags implements Articles {
    private String name;
    private List<Articles> bags=new ArrayList<>();
    public Bags(String name){
        this.name=name;
    }
    public void add(Articles a){
        bags.add(a);
    }
    public void remove(Articles a){
        bags.remove(a);
    }
    public Articles getChild(int i){
        return bags.get(i);
    }

    @Override
    public float calculation() {
        AtomicReference<Float> sum= new AtomicReference<>((float) 0);
        bags.forEach(item-> sum.updateAndGet(v -> (float) (v + item.calculation())));
        return sum.get();
    }

    @Override
    public void show() {
        bags.forEach(Articles::show);
    }
}
